package top.blackr.folivora.common.security.captcha;

import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.captcha.service.ICaptchaService;
import org.springframework.web.bind.annotation.*;
import top.blackr.folivora.common.core.R;

/**
 * <a href="https://gitee.com/596392912/mica/tree/master/mica-captcha">验证码<a/>
 */
@RestController
@AllArgsConstructor
@RequestMapping("captcha")
public class CaptchaController {

    private final ICaptchaService captchaService;

    @GetMapping
    @Operation(summary = "Base64验证码", description = "Base64验证码")
    public R<String> getCaptcha(@RequestParam("repCode") String uuid) {
        String captchaBase64 = captchaService.generateBase64(uuid);
        return R.ok(captchaBase64);
    }

    @PostMapping("validate")
    @Operation(summary = "验证码校验", description = "验证码校验")
    public R<Boolean> validateCode(@RequestParam("repCode") String uuid, @RequestParam("code") String userInputCaptcha) {
        boolean validate = captchaService.validate(uuid, userInputCaptcha);
        return R.ok(validate);
    }
}
